package com.cqgc.controller;

import com.cqgc.aop.LogAnnotation;
import com.cqgc.pojo.dto.LogsDTO.LogsParm;
import com.cqgc.pojo.dto.RoleDTO.RoleParm;
import com.cqgc.pojo.entity.PageResult;
import com.cqgc.pojo.entity.Result;
import com.cqgc.service.LogsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/logs")
@Api(tags = "日志接口")
public class LogsController {
    @Autowired
    private LogsService logsService;

    @ApiOperation("查询")
    @GetMapping("/list")
    @LogAnnotation(module="日志",operator="查询日志")
    public Result<PageResult> list( LogsParm logsParm)
    {
        PageResult pageResult = logsService.list(logsParm);
        if (pageResult != null){
            return Result.success(new PageResult(pageResult.getTotal(),pageResult.getRecords()));
        }
        return Result.error();
    }
    //删除
    @ApiOperation("删除日志")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        if (id == 0) {
            return Result.error("请选择要删除的角色!");
        }
        if (logsService.removeById(id)) {
            return Result.success("删除角色成功!");
        }
        return Result.error("删除角色失败!");
    }

    @ApiOperation("删除多条日志")
    @PostMapping("/delete")
    public Result deleteByIds(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return Result.error("请选择要删除的日志!");
        }
        int i = logsService.deleteByIds(ids);
        System.out.println(i+"aaa");
        if (i >1) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

}